<template>
  <div>
    <!-- 跟进记录弹出框 -->
    <a-drawer
      title="跟进记录"
      :visible="visible"
      :width="800"
      placement="right"
      @close="closeLogInfo"
    >
      <!-- 新建跟进记录 -->
      <div>
        <div class="log-title">新建跟进记录</div>
        <div class="log-content">
          <a-row class="log-content_item">
            <a-col :span="3" class="log-content_item-label">客户手机号：</a-col>
            <a-col :span="12" class="log-content_item-desc">{{ current.phonenumber }}</a-col>
          </a-row>
          <a-row class="log-content_item">
            <a-col :span="3" class="log-content_item-label">意向打分：</a-col>
            <a-col :span="12" class="log-content_item-desc">
              <a-rate v-model:value="logInfo.score"></a-rate>
            </a-col>
          </a-row>
          <a-row class="log-content_item log-content_item-textarea">
            <a-col :span="3" class="log-content_item-label">备注：</a-col>
            <a-col :span="12" class="log-content_item-desc">
              <a-textarea
                v-model:value="logInfo.remark"
                placeholder="请输入"
                :auto-size="{ minRows: 2, maxRows: 5 }"
              />
            </a-col>
          </a-row>
          <a-row type="flex" justify="end">
            <a-button type="primary" @click="confirmAddFollowLog">确定</a-button>
          </a-row>
        </div>
      </div>
      <!-- 历史跟进记录 -->
      <div style="border-bottom: 1px dashed #ddd; margin: 20px 0"></div>
      <div class="log-title">历史跟进记录</div>
      <div v-if="logList.length" style="padding-top: 20px; margin-bottom: 60px">
        <a-timeline>
          <a-timeline-item v-for="item in logList" :key="item.followId">
            <div style="font-size: 16px; color: #999">{{ item.followTimeStr }}</div>
            <div class="history-content">
              <div class="history-item">
                <div class="history-item_title">跟进人：</div>
                <div class="history-item_desc">{{ item.folperNumber }}</div>
              </div>
              <div class="history-item">
                <div class="history-item_title">意向打分：</div>
                <div class="history-item_desc">
                  <a-rate :value="item.score" disabled></a-rate>
                </div>
              </div>
              <div class="history-item">
                <div class="history-item_title history-item_title-remark">备注：</div>
                <div class="history-item_desc">{{ item.remark }}</div>
              </div>
            </div>
          </a-timeline-item>
        </a-timeline>
      </div>
      <a-row v-else type="flex" justify="center" align="middle" style="color: #999; padding: 50px">
        暂无跟进
      </a-row>
      <div
        :style="{
          position: 'absolute',
          right: 0,
          bottom: 0,
          width: '100%',
          borderTop: '1px solid #e9e9e9',
          padding: '10px 16px',
          background: '#fff',
          textAlign: 'right',
          zIndex: 1,
        }"
      >
        <a-button style="margin-right: 8px" @click="closeLogInfo">取消</a-button>
      </div>
      <div style="height: 60px"></div>
    </a-drawer>
  </div>
</template>
<script>
import { defineComponent, reactive, toRefs, watch } from 'vue';
import { apiGetClientFollowList, apiAddFollowLog } from '@/api/clientManage/account';
import { message } from 'ant-design-vue';
import { parseTime, isEmptyObject } from '@/utils';
const transFollowList = list => {
  return list.map(item => {
    item.followTimeStr = item.followTime && parseTime(item.followTime);
    return item;
  });
};

export default defineComponent({
  name: '',
  props: {
    visible: {
      default: false,
      type: Boolean,
    },
    current: {
      default: () => ({}),
      type: Object,
    },
  },
  emits: {
    'update:visible': null,
    'close-log': null,
    getList: null,
  },
  setup(props, { emit }) {
    const state = reactive({
      logList: [], // 跟进列表
      logInfo: {
        // 提交信息
        score: 0,
        remark: '',
      },
    });
    let isLoading = false;

    /**
     * 关闭记录弹窗
     **/
    const closeLogInfo = () => {
      emit('update:visible', false);
      emit('close-log');
      resetLogInfo();
    };

    /**
     * 获取用户跟进列表
     **/
    const { current } = toRefs(props);
    const getList = () => {
      const { userId } = current.value;
      apiGetClientFollowList({ userId }).then(res => {
        state.logList = transFollowList(res.dataInfo);
      });
    };
    watch(current, key => {
      if (isEmptyObject(key)) return;
      getList();
    });

    /**
     * 重置记录信息
     **/
    const resetLogInfo = () => {
      state.logInfo.score = 0;
      state.logInfo.remark = '';
      state.logList = [];
    };

    /**
     * 发送请求添加跟进记录
     **/
    const confirmAddFollowLog = async () => {
      if (isLoading) return;
      const { userId } = current.value;
      const { score, remark } = state.logInfo;
      try {
        isLoading = true;
        await apiAddFollowLog({
          userId,
          score,
          remark,
        });
        getList();
        resetLogInfo();
        message.success('新增成功');
        emit('getList');
      } finally {
        isLoading = false;
      }
    };

    return {
      ...toRefs(state),
      closeLogInfo,
      confirmAddFollowLog,
    };
  },
});
</script>
<style lang="less" scoped>
.log {
  padding-left: 20px;
  &-title {
    position: relative;
    margin-bottom: 20px;
    padding-left: 12px;
    font-weight: 700;
    font-size: 16px;
    &::before {
      position: absolute;
      top: 6px;
      left: 0;
      display: block;
      width: 3px;
      height: 14px;
      background-color: #1890ff;
      border-radius: 2px;
      content: '';
    }
  }
  &-content {
    &_item {
      display: flex;
      align-items: center;
      height: 50px;
      &-label {
        margin-right: 20px;
        text-align: right;
      }
    }
    &_item-textarea {
      height: 100px;
    }
  }
}
.history-content {
  position: relative;
  width: 600px;
  margin-bottom: 10px;
  padding: 30px 0 10px 0;
  background-color: #f9f9f9;
  .history-item {
    display: flex;
    align-content: center;
    margin-bottom: 10px;
    &_title {
      display: flex;
      align-items: flex-end;
      justify-content: flex-end;
      width: 90px;
      margin-right: 20px;
      &-remark {
        align-items: center;
      }
    }
    &_desc {
      flex: 1;
    }
  }
}
.history-content_line {
  position: absolute;
  top: 0;
  left: 0;
  border-left: 1px solid #000;
}
</style>
